home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / Programming / THXPlayLib / src / library.asm < prev    next >
Encoding:
Assembly Source File  |  1998-05-11  |  3.5 KB  |  190 lines

  1. ; based on sample.library
  2.  
  3.     STRUCTURE ThxPlayBase,LIB_SIZE
  4.     UBYTE    thx_Flags
  5.     UBYTE    thx_pad
  6.     ULONG    thx_SegList
  7.     APTR    thx_OwnerTask        ; task that owns THX
  8.     STRUCT    thx_Sem,SS_SIZE        ; semaphore to protect the above
  9.     LABEL    ThxPlayBase_SIZEOF
  10.  
  11.     moveq    #-1,d0
  12.     rts
  13.  
  14. RomTag    dc.w    RTC_MATCHWORD
  15.     dc.l    RomTag,EndCode
  16.     dc.b    RTF_AUTOINIT,VERSION,NT_LIBRARY,0
  17.     dc.l    LibName,IDStr,InitTbl
  18.  
  19. LibName    dc.b    'thxplay.library',0,'$VER: '
  20. IDStr    VSTRING
  21.     ifd    USE020
  22.     dc.b    '68020 version',0
  23.     endc
  24.     cnop    0,4
  25.  
  26. InitTbl    dc.l    ThxPlayBase_SIZEOF,funcTbl,dataTbl,Init
  27.  
  28. func    macro
  29.     dc.w    \1 - funcTbl
  30.     endm
  31. funcTbl    dc.w    -1
  32.     func    Open
  33.     func    Close
  34.     func    Expunge
  35.     func    Null
  36.  
  37. ; our functions start here
  38.     func    thxInit_library
  39.     func    thxFree_library
  40.  
  41.     func    thxPlay
  42.     func    thxStop
  43.     func    thxPause
  44.     func    thxWind
  45.  
  46.     func    thxGetVolume
  47.     func    thxSetVolume
  48.  
  49.     func    thxGetNumSongs
  50.     func    thxSetSong
  51.  
  52.     func    thxPlayNote
  53.     func    thxStopNote
  54.     func    thxNoteFX
  55.     func    Null                ; reserved slot
  56.  
  57.     func    thxSignalEnd
  58.     func    thxSongEnded
  59.     func    thxSyncByte
  60.     func    thxPlaytime
  61.  
  62.     dc.w    -1
  63.  
  64.  
  65. dataTbl    INITBYTE  LN_TYPE,    NT_LIBRARY
  66.     INITLONG  LN_NAME,    LibName
  67.     INITBYTE  LIB_FLAGS,    LIBF_SUMUSED!LIBF_CHANGED
  68.     INITWORD  LIB_VERSION,    VERSION
  69.     INITWORD  LIB_REVISION,    REVISION
  70.     INITLONG  LIB_IDSTRING,    IDStr
  71.     dc.l    0
  72.  
  73. *--------------------------------------
  74.  
  75. Init    movem.l    a0/a1/d1/a5,-(sp)
  76.     move.l    d0,a5
  77.     move.l    a0,thx_SegList(a5)
  78.     lea    thx_Sem(a5),a0
  79.     jsr    _LVOInitSemaphore(a6)
  80.  
  81.     ifd    USE020
  82.     move.w    AttnFlags(a6),d1
  83.     moveq    #0,d0
  84.     btst.b    #AFB_68020,d1
  85.     beq.s    .exit
  86.     endc
  87.  
  88.     move.l    a5,d0
  89. .exit    movem.l    (sp)+,a0/a1/d1/a5
  90.     rts
  91.  
  92. *--------------------------------------
  93.  
  94. Open    addq.w    #1,LIB_OPENCNT(a6)
  95.     bclr    #LIBB_DELEXP,thx_Flags(a6)
  96.     move.l    a6,d0
  97.     rts
  98.  
  99. Close    moveq    #0,d0
  100.     subq.w    #1,LIB_OPENCNT(a6)
  101.     bne.s    .noexp
  102.     btst    #LIBB_DELEXP,thx_Flags(a6)
  103.     beq.s    .noexp
  104.     bsr.s    Expunge
  105. .noexp    rts
  106.  
  107. Expunge    movem.l    d2/a5/a6,-(sp)
  108.     move.l    a6,a5
  109.     move.l    4.w,a6
  110.     tst.w    LIB_OPENCNT(a5)
  111.     beq    .close
  112.     bset    #LIBB_DELEXP,thx_Flags(a5)
  113.     moveq    #0,d0
  114.     bra.s    .noexp
  115. .close    move.l    thx_SegList(a5),d2
  116.     move.l    a5,a1
  117.     move.l    4.w,a6
  118.     jsr    _LVORemove(a6)
  119.     moveq    #0,d0
  120.     move.l    a5,a1
  121.     move.w    LIB_NEGSIZE(a5),d0
  122.     sub.l    d0,a1
  123.     add.w    LIB_POSSIZE(a5),d0
  124.     jsr    _LVOFreeMem(a6)
  125.     move.l    d2,d0
  126. .noexp    movem.l    (sp)+,d2/a5/a6
  127.     rts
  128.  
  129. Null    moveq    #0,d0
  130.     rts
  131.  
  132. *--------------------------------------
  133.  
  134. ; special semaphore/owner based wrappers for thxInit() and thxFree()
  135.  
  136. thxInit_library
  137.     movem.l    a0/a5/a6,-(sp)
  138.     move.l    a6,a5
  139.     move.l    4.w,a6
  140.     lea    thx_Sem(a5),a0
  141.     jsr    _LVOObtainSemaphore(a6)
  142.     suba.l    a1,a1
  143.     jsr    _LVOFindTask(a6)    ; d0 = ourselves
  144.  
  145.     lea    thx_OwnerTask(a5),a0
  146.     cmp.l    (a0),d0            ; check if owner = ourselves
  147.     beq.s    .got            ; continue OK if we own THX
  148.     tst.l    (a0)            ; otherwise, see if owner = nobody
  149.     beq.s    .take            ; if noone owns THX, we take it
  150.  
  151.     lea    thx_Sem(a5),a0        ; otherwise, we fail to thxInit()
  152.     jsr    _LVOReleaseSemaphore(a6)
  153.     movem.l    (sp)+,a0/a5/a6
  154.     moveq    #-1,d0    ; FAIL
  155.     rts
  156.  
  157. .take    move.l    d0,(a0)            ; set owner = ourselves
  158. .got    lea    thx_Sem(a5),a0
  159.     jsr    _LVOReleaseSemaphore(a6)
  160.     movem.l    (sp)+,a0/a5/a6
  161.  
  162.     bsr    thxInit            ; call the real thxInit()
  163.     tst.l    d0
  164.     beq.s    .exit            ; if this fails, also call
  165.     bsr    thxFree_library        ; our redefined thxFree()
  166. .exit    rts
  167.  
  168.  
  169. thxFree_library
  170.     movem.l    a5/a6,-(sp)
  171.     move.l    a6,a5
  172.     move.l    4.w,a6
  173.     lea    thx_Sem(a5),a0
  174.     jsr    _LVOObtainSemaphore(a6)
  175.     suba.l    a1,a1
  176.     jsr    _LVOFindTask(a6)    ; d0 = ourselves
  177.  
  178.     move.l    thx_OwnerTask(a5),d1    ; d1 = owner
  179.     cmp.l    d0,d1            ; do we own THX?
  180.     bne.s    .exit            ; no! don't allow freeing of THX
  181.     bsr    thxFree            ; otherwise, call the real thxFree()
  182.     clr.l    thx_OwnerTask(a5)    ; then set owner = nobody
  183.  
  184. .exit    lea    thx_Sem(a5),a0
  185.     jsr    _LVOReleaseSemaphore(a6)
  186.     movem.l    (sp)+,a5/a6
  187.     rts
  188.  
  189. EndCode
  190.